package cn.hsa.fqz.task.controller;

import cn.hsa.fqz.handler.base.BaseController;
import cn.hsa.fqz.task.entity.RegionDO;
import cn.hsa.fqz.task.enums.UserCategoryEnums;
import cn.hsa.fqz.task.service.RegionService;
import cn.hsa.fqz.task.service.dto.LoginUserDTO;
import cn.hsa.fqz.util.UserRegionUtils;
import cn.hsa.hsaf.core.framework.context.HsafContextHolder;
import cn.hsa.hsaf.core.framework.util.CurrentUser;
import cn.hsa.hsaf.core.framework.web.WrapperResponse;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description TODO
 * @ClassName LoginController
 * @Author Liu.Jun
 * @Create 2023-04-22 15:14
 * @Version 1.0.0
 */
@RequestMapping("/fqz")
@RestController
@Slf4j
@Api(tags = {"获取区域信息"})
public class RegionController extends BaseController {

    @Autowired
    private RegionService regionService;

    @ApiOperation(value = "获取行政区划JSON", notes = "获取行政区划JSON")
    @GetMapping("/list/province")
    public WrapperResponse<String> listProvince(){
        LambdaQueryWrapper<RegionDO> queryWrapper = new LambdaQueryWrapper<>();
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO dto = super.getLoginUser(currentUser);
        String admdvs = dto.getRegion();
        //admdvs = "430000";
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(admdvs);
        if(enums == UserCategoryEnums.GLOBAL){
            queryWrapper.eq(RegionDO::getpRegion,"999");
        }else {
            queryWrapper.eq(RegionDO::getpRegion,admdvs);
        }
        List<RegionDO> regionDOS = regionService.list(queryWrapper);
        List<RegionDO> result = Lists.newArrayList();
        for (RegionDO source: regionDOS) {
            RegionDO target = new RegionDO();
            target.setRegion(source.getRegion());
            if(enums == UserCategoryEnums.GLOBAL){
                target.setProvice(source.getProvice());
            }else{
                target.setProvice(source.getCity());
            }
            result.add(target);
        }
        return WrapperResponse.success(JSON.toJSONString(result));
    }

    @ApiOperation(value = "通过父级id获取子级行政区划", notes = "通过父级id获取子级行政区划")
    @GetMapping("/list/region")
    public WrapperResponse<List<RegionDO>> listByPregion(@RequestParam("pRegion") String region){
        List<RegionDO> regionDOS = regionService.getRegionsBypRegion(region);
        return WrapperResponse.success(regionDOS);
    }

    @ApiOperation(value = "通过父级id获取子级行政区划", notes = "通过父级id获取子级行政区划")
    @GetMapping("/current/list/province")
    public WrapperResponse<List<RegionDO>> listByPregion(){
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO dto = super.getLoginUser(currentUser);
        String admdvs = dto.getRegion();
        List<RegionDO> regionDOS = Lists.newArrayList();
        //admdvs = "430000";
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(admdvs);
        LambdaQueryWrapper<RegionDO> queryWrapper = new LambdaQueryWrapper<>();
        if(enums == UserCategoryEnums.GLOBAL){
           regionDOS = regionService.getRegionsBypRegion("999");
        }else {
            queryWrapper.eq(RegionDO::getRegion,admdvs);
            regionDOS =  regionService.list(queryWrapper);
        }
        return WrapperResponse.success(regionDOS);
    }

    @ApiOperation(value = "获取登录用户类型(GLOBAL/PROVINCE/CITY)", notes = "获取登录用户类型(GLOBAL/PROVINCE/CITY)")
    @GetMapping("/user/category")
    public WrapperResponse<String> getCurrentUserCategory(){
        CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();
        LoginUserDTO dto = getLoginUser(currentUser);
        UserCategoryEnums enums = UserRegionUtils.getUserCateEnumByAscm(dto.getRegion());
        return WrapperResponse.success(enums.name());
    }

    @ApiOperation(value = "获取所有城市", notes = "获取所有城市")
    @GetMapping("/all/city")
    public WrapperResponse<List<RegionDO>> getAllCitys(){
        List<RegionDO> citys = regionService.getAllCitys();
        return WrapperResponse.success(citys);
    }
}
